from random import random
from math import cos, sin, pi, sqrt

class ConvexHull:

    def __init__(self):
        self.points = []
        self.edges = []
        
    def clear(self):
        self.points = []
        self.edges = []
        
    def generateConvexHull(self, ch_type):
        if ch_type == "JARVIS":
            self.edges = Jarvis().generateConvexHull(self.points)
        elif ch_type == "SCAN":
            self.edges = Scan().generateConvexHull(self.points)        
        
class Jarvis:

    def generateConvexHull(self, points):
        edges = []
        x0 = x1 = points[0][0]
        y0 = y1 = points[0][1]
        
        for point in points:
            if x0 > point[0]:
                x0 = point[0]
            if x1 < point[0]:
                x1 = point[0]
            if y0 > point[1]:
                y0 = point[1]
            if y1 < point[1]:
                y1 = point[1]
                
        edges.append([x0,y0])
        edges.append([x1,y0])
        edges.append([x1,y1])
        edges.append([x0,y1])
        return edges
        
class Scan:

    def generateConvexHull(self, points):
        edges = []
        x0 = y0 = points[0][0]
        x1 = y1 = points[0][1]        
        
        for point in points:
            if x0 > point[0]:
                x0 = point[0]
            if x1 < point[0]:
                x1 = point[0]
            if y0 > point[1]:
                y0 = point[1]
            if y1 < point[1]:
                y1 = point[1]
                
        edges.append([x0,y0])
        edges.append([x1,y0])
        edges.append([x1,y1])
        edges.append([x0,y1])
        return edges         


class Region:

    def generateSpace(self, quantity):
        elements = []
        for i in range(quantity):
            x = -5 + (random()*10)
            y = -5 + (random()*10)            
            elements.append( (x,y) )
        return elements

    def generateDisk(self, quantity):
        elements = []
        for i in range(quantity):
            angle = (random()*2*pi)
            lenght = random()*3
            x = lenght * cos(angle)
            y = lenght * sin(angle)
            elements.append( (x,y) )
        return elements

    def generateCircle(self, quantity):
        elements = []
        for i in range(quantity):
            angle = (random()*2*pi)
            lenght = 3
            x = lenght * cos(angle)
            y = lenght * sin(angle)
            elements.append( (x,y) )
        return elements        


    def generateTriangle(self, quantity):
        elements = []
        for i in range(quantity):
            a1 = random()
            a2 = random()
            a3 = random()

            p1 = [ 0.0,            3.0 ]
            p2 = [-2.0 * sqrt(3), -3.0 ]
            p3 = [ 2.0 * sqrt(3), -3.0 ]
            
            x = (a1*p1[0] + a2*p2[0] + a3*p3[0]) / (a1 + a2 + a3)
            y = (a1*p1[1] + a2*p2[1] + a3*p3[1]) / (a1 + a2 + a3)
            elements.append( (x,y) )
        return elements

    def generateSquare(self, quantity):
        elements = []
        for i in range(quantity):
            x = -3 + (random()*6)
            y = -3 + (random()*6)            
            elements.append( (x,y) )
        return elements        
